package com.mobile.security.webres;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

import org.acegisecurity.ConfigAttribute;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.intercept.web.FilterInvocation;
import org.acegisecurity.intercept.web.FilterInvocationDefinitionSource;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import com.mobile.business.grid.IBaseBusiness;
import com.mobile.model.SysWebresPermission;

public class RdbmsFilterInvocationDefinitionSource implements
		FilterInvocationDefinitionSource {
	private final Logger LOGGER = LogManager
			.getLogger(RdbmsFilterInvocationDefinitionSource.class);
	
	private IBaseBusiness baseBusiness;
	
	private Ehcache webresdbCache;
	
	private PathMatcher pathMatcher = new AntPathMatcher();
	
	private boolean useCache = true;
	
	
	public boolean isUseCache() {
		return useCache;
	}
	public void setUseCache(boolean useCache) {
		this.useCache = useCache;
	}
	/**
	 * 获取web权限信息
	 */
	public ConfigAttributeDefinition getAttributes(Object object)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		if ((object == null) || !this.supports(object.getClass())) {
			throw new IllegalArgumentException("抱歉，目标对象不是FilterInvocation类型");
		}
		
		String url = ((FilterInvocation)object).getRequestUrl();
		List<SysWebresPermission> list = this.getSysWebresPermissions();
		
		if(list==null||list.size()==0){
			return null;
		}
		
		int firstQuestionMarkIndex = url.indexOf("?");
		if(firstQuestionMarkIndex!=-1){
			url = url.substring(0, firstQuestionMarkIndex);
		}
		
		ConfigAttributeDefinition  cad = new ConfigAttributeDefinition();
		boolean hasCad = false;
		for(SysWebresPermission swp : list){
			boolean matched = this.pathMatcher.match(swp.getUrl(), url);
			if(matched){
				ConfigAttributeDefinition caTemp = swp.getCad();
				Iterator<ConfigAttribute> iter= caTemp.getConfigAttributes();
				while(iter.hasNext()){
					cad.addConfigAttribute(iter.next());
				}
				if(!hasCad){
					hasCad = true;
				}
			}
		}
		
		if(hasCad){
			return cad;
		}else{
			return null;
		}
	}
	/**
	 * 获取所有的权限信息
	 */
	public Iterator getConfigAttributeDefinitions() {
		// TODO Auto-generated method stub
		Set set = new HashSet();
		
		List<SysWebresPermission> list = this.getSysWebresPermissions(); 
		for(SysWebresPermission swp :list){
			set.add(swp.getCad());
		}
		
		return set.iterator();
	}
	/**
	 * 获取web资源的权限信息
	 * @return	信息列
	 */
	private List getSysWebresPermissions(){
		List<SysWebresPermission> list = null;
		Element element = this.webresdbCache.get("webres");
		if(element!=null&&useCache){
			list = (List)element.getValue();
		}else{
			list = baseBusiness.findByExample(new SysWebresPermission());
			Element elem = new Element("webres",list);
			this.webresdbCache.put(elem);
		}
		return list;
	}
	
	public boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		if(FilterInvocation.class.isAssignableFrom(clazz)){
			return true;
		}else{
			return false;
		}
	}

	public IBaseBusiness getBaseBusiness() {
		return baseBusiness;
	}

	public void setBaseBusiness(IBaseBusiness baseBusiness) {
		this.baseBusiness = baseBusiness;
	}

	public Ehcache getWebresdbCache() {
		return webresdbCache;
	}

	public void setWebresdbCache(Ehcache webresdbCache) {
		this.webresdbCache = webresdbCache;
	}

}
